<?php

/**
 * 用户的某些操作可能会增加网盘容量
 * do_type=0:login;1:comment;2:upload
 * login每天有一次获取空间的机会
 * comment每天有三次获取空间的机会
 * upload不限量
 */
class DiskGenAR extends CActiveRecord{
	public static function model($className=__CLASS__){
		return parent::model($className);
	}

	public static function tblName()	{
		return 'ccs8_disk_gen';
	}
	
	public function tableName()	{
		return self::tblName();
	}
	
	public static function topNew($limit  = 5){
		$sql = 'select user_name,doing,gen_size from '.self::tblName();
		$sql .= ' order by id desc limit '.$limit;
		return DBUtil::queryAll($sql);
	}

    public static function saveRecord($userId = 0,$userName='Guest',$doType=0,$doing='',$genSize=0,$today=0){
        $sql = 'insert into '.self::tblName().'(fk_user,user_name,do_type,doing,gen_size,today) ';        
        $sql .= 'values(:uid,:name,:dtype,:doing,:size,:today)';
        $cmd = DBUtil::cmd($sql);
        $cmd->bindParam(":uid",$userId,PDO::PARAM_INT);
        $cmd->bindParam(":name",$userName,PDO::PARAM_STR);
        $cmd->bindParam(":dtype",$doType,PDO::PARAM_INT);
        $cmd->bindParam(":doing",$doing,PDO::PARAM_STR);
        $cmd->bindParam(":size",$genSize,PDO::PARAM_INT);
        $cmd->bindParam(":today",$today,PDO::PARAM_INT);
        $cmd->execute();
    }
    
    public static function forLogin($userId = 0,$userName = 'Guest'){
    	$genSize = rand(1,5);
    	self::saveRecord($userId,$userName,0,'签到成功',$genSize,MyUtil::today());
    	return $genSize;
    }
    
    public static function forComment($userId = 0,$userName = 'Guest'){
    	$genSize = rand(1,5);
    	self::saveRecord($userId,$userName,1,'发表了评论',$genSize,MyUtil::today());
    	return $genSize;
    }
    
    public static function forUpload($userId = 0,$userName = 'Guest'){
    	$genSize = rand(1,5);
    	self::saveRecord($userId,$userName,2,'上传了一本书',$genSize,MyUtil::today());
    	return $genSize;
    }

	/**
	 * 刚开始是想一旦登录就可以获取空间，后来是想必须手工签到才能获取空间
	 * 不过都是通过这一个方法来判断是否获取过空间了
	 */
    public static function isLoginToday($userId = 0){
        $sql = 'select count(id) from '.self::tblName();
        $sql .= ' where fk_user='.$userId.' and do_type=0 and today='.MyUtil::today();
        $count = DBUtil::queryScalar($sql);
        return intval($count);
    }
    
    public static function commentCanGenDiskToday($userId = 0){
    	$sql = 'select count(id) from '.self::tblName();
        $sql .= ' where fk_user='.$userId.' and do_type=1 and today='.MyUtil::today();
        $count = DBUtil::queryScalar($sql);
        return intval($count) < 3;
    }
}

